package com.yipingnai.blog.aop;

import com.yipingnai.blog.utils.RedisUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: 一瓶奶
 * @Url: www.yipingnai.cn
 * @Date: 2020/6/19 9:29
 */
@Component
@Aspect
public class BlogPV {

    @Autowired
    RedisUtils redisUtils;
    @Pointcut("execution(* com.yipingnai.blog.controller.ArticleContoller.getDetail(..))")
    public void blog(){

    }

    @After("blog()")
    public void blogAfter(JoinPoint jp){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        Object[] args = jp.getArgs();
        String host = request.getRemoteHost();
        String key = "blogid:" + args[0];
        if(redisUtils.isExitField("blogpv",key)){
            Long blogpv = redisUtils.updateFieldValue("blogpv", key, 1);
        }else {
            boolean blogpv = redisUtils.setField("blogpv", key);
        }
    }

}
